// pages/withdrawals/withdrawals.js
var app = getApp();
var model = require('../../utils/model/model.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    charge: '',
    bankCard: '',
    userInfo: '',
    accountTotal: '',
    canSubmit: false,
    modelData: {
      showModal: false,
      cancelText: '取消',
      confirmText: "确定",
      content: '是否确定提现',
      confirmCallback: 'confirmCallback',
    },
    notRawals: 2,
    loadSuccess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    model.init.apply(this, ['modelData']);
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    wx.showLoading();
    this.getMyBankCard();
    this.getAccountTotal('accountTotal', 2);
    this.setData({
      'modelData.showModal': false,
      'modelData.content': '您有一个提现正在进行中，目前无法申请提现',
      'modelData.signle': true,
      'modelData.signleBtnTxt': '确定',
      'modelData.confirmCallback': ''
    })

  },
  changeInput: function (e) {
    this.setData({
      charge: e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
    })
    var canSubmit=true;
    if (this.data.charge == ''){
      canSubmit=false;
    }
    if (this.data.charge <= 0) {
      canSubmit = false;
    }
    this.setData({
      canSubmit: canSubmit
    })
  },
  allWithdrawals: function () {
    this.setData({
      charge: this.data.accountTotal
    })
    if (this.data.accountTotal>0){
      this.setData({
        canSubmit: true
      })
    }
  },
  getMyBankCard: function () {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyBankCard/getOne', //仅为示例，并非真实的接口地址
      data: {
        userId: this.data.userInfo.id
      },
      success: function (res) {
        if (res.data.success) {
          res.data.data.card = res.data.data.cardNumber.substring(res.data.data.cardNumber.length - 4, res.data.data.cardNumber.length);
          that.setData({
            bankCard: res.data.data
          })
        }
      }
    })
  },
  confirmCallback: function () {
    this.setData({
      canSubmit:false
    })
    wx.showLoading({
      title: '提现中',
    })
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'fastMoneyRefundRecord/save', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        bankId: this.data.bankCard.bankNo,
        applicant: this.data.bankCard.cardHolder,
        cardNumber: this.data.bankCard.cardNumber,
        amount: this.data.charge,
        userId: this.data.userInfo.id
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.success) {
          wx.redirectTo({
            url: "/pages/rechargeDetail/rechargeDetail",
          })
        } else {
          that.setData({
            'modelData.showModal': true,
            'modelData.content': res.data.error,
            'modelData.signle': true,
            'modelData.confirmCallback': '',
            canSubmit: true
          })
        }
      }
    })
  },
  getAccountTotal: function (key,adds) {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'userPayInfo/userAccount', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        userId: this.data.userInfo.id,
        adds: adds
      },
      success: function (res) {
        if (res.data.success) {
          if(adds==1){
            wx.hideLoading();
            that.setData({ 
              loadSuccess:true,
              [key]: (res.data.data * 1000 - that.data.accountTotal * 1000)/1000
            })
          }else{
            that.getAccountTotal('notRawals', 1);
            that.setData({
              [key]: res.data.data
            })
          } 
        }
      }
    })
  },
  showReson: function (e) {
    this.setData({
      'modelData.showModal': true,
      'modelData.content': '交易未完成',
      'modelData.signle': true,
      'modelData.confirmCallback': ''
    })
  },
  save: function (e) {
    if (this.data.canSubmit) {
      if (parseFloat(this.data.charge) > parseFloat(this.data.accountTotal)) {
        this.setData({
          'modelData.showModal': true,
          'modelData.content': '输入金额大于可提现金额',
          'modelData.signle': true,
          'modelData.confirmCallback': ''
        })
      } else {
        this.setData({
          'modelData.showModal': true,
          'modelData.content': '是否确定提现',
          'modelData.signle': false,
          'modelData.confirmCallback': 'confirmCallback'
        })
      }
    }
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})